<!DOCTYPE html>
<html>

	<head lang="en">
		<title>游记评论</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
		<script src="/js/jquery/jquery.min.js"></script>
		<script src="/js/bootstrap/js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="/css/font/css/font-awesome.min.css" />
		<link rel="stylesheet" href="/css/reset.css" />
		<link rel="stylesheet" href="/css/strategyComment.css" />
		<script src="/js/plugins/jrender/jrender.js"></script>
		<link rel="stylesheet" href="/js/plugins/jquery-confirm/css/jquery-confirm.css">
        <link rel="stylesheet" href="/js/plugins/dialog/dialog.css"/>
        <script src="/js/plugins/dialog/dialog.min.js"></script>
		<script src="/js/plugins/jquery-confirm/js/jquery-confirm.js"></script>
		<script src="/js/common.js"></script>
		
		<script>
			$(function () {
                //获取当前点评攻略的id
                var travelId = getParams().travelId;

                //获取当前用户
                var user = JSON.parse(sessionStorage.getItem("user"));

                //查询全部评论
                $.get("/travels/"+travelId+"/replyCount",function (data) {
                    $("#replyCount").html("全部评论("+data.replyCount+")");
                })

                //渲染评论
                var currentPage = 1;//当前页
                var pages;//总页数
                var replies;//评论数组
                $.get("/travels/" + travelId + "/replies", {pageSize: 4, state: 1}, function (data) {
                    //渲染评论页面
                    if(data.list.length){
                        renderReplyView(data);
                    }else {
                        $(".replies").html("快来发表你的评论吧").addClass("comment-list");
                    }
                    pages = data.pages;
                    replies = data.list;
                })

                //评论分页
                $(window).scroll(function () {
                    // 判断是否触底
                    if (isEnd()) {
                        // 判断是否还有下一页
                        if (currentPage < pages) {
                            // 当前页+1
                            currentPage++;
                            // 发送ajax请求
                            var args = {state: 1, currentPage: currentPage, pageSize: 4};
                            $.get("/travels/" + travelId + "/replies", args, function (data) {
                                // 拼接游记评论数组数据
                                $.merge(replies, data.list);
                                // 渲染页面
                                if(replies){
                                    renderReplyView({list: replies});
                                }else {
                                    $(".replies").html("快来发表你的评论吧").addClass("comment-list");
                                }
                            });
                        } else {
                            // 否则, 提示已到低
                            $(document).dialog({
                                type: 'notice',
                                infoText: '已到底部',
                                autoClose: 1500,
                                position: 'bottom'  // center: 居中; bottom: 底部
                            });
                        }
                    }
                });

                //点击添加你的评论跳转页面
                $("#addReply").click(function () {
                    if(user) {
                        window.location.href = "/mine/addTravelReply.html?travelId="+travelId;
                    }else {
                        // 跳转到登录页面进行登录操作
                        $(document).dialog({
                            type: "confirm",
                            titleShow: false,
                            content: '您尚未登录',
                            buttons: [
                                {
                                    name: '跳转到登录页面',
                                    callback: function () {
                                        location.href = "/login.html";
                                    }
                                }
                            ]
                        });
                    }
                })

            })

            //渲染评论的方法
            function renderReplyView(data) {
                $(".replies").renderValues(data, {
                    gethref: function (ele, value) {
                        ele.href = "/userProfiles.html?authorId=" + value;
                    }
                });
            }


        </script>
	</head>

	<body>
		<div class="search-head">
			<div class="row nav-search">
				<div class="col">
					<a href="javascript:;" onclick="window.history.go(-1)">
						<span><i class="fa fa-chevron-left"></i></span>
					</a>
				</div>
				<div class="col">
					<span id="replyCount">全部评论</span>
				</div>
				<div class="col"></div>
			</div>
		</div>
		
		<div class="replies">
            <div class="list-group" render-loop="list">
                <div class="list-group-item">
                    <div class="container row comment">
                        <div class="col-2 comment-head">
                            <a render-key="list.replier.id" render-fun="gethref">
                                <img class="rounded-circle"
                                     render-src="list.replier.headImgUrl">
                            </a>
                        </div>
                        <div class="col comment-right">
                            <p class="authorName"><span render-html="list.replier.nickName"></span></p>
                            <span class="comment-date" render-html="list.createTime"></span>
                            <div class="comment-content">
                                <p render-html="list.content">
                                </p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
		</div>

		<div class="operation">
			<div class="d-flex justify-content-between">
                <div class="p-2"></div>
                <div class="p-2" id="addReply"><i class="fa fa-commenting-o"></i> 添加你的评论</div>
			</div>
		</div>

</body>

</html>